﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MessageBox.cs" company="arvystate.net">
//   arvystate.net
// </copyright>
// <summary>
//   The message box.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace PresentationAid.Dialog
{
    using System.Windows;

    using NLog;

    /// <summary>
    /// The message box.
    /// </summary>
    public sealed class MessageBox
    {
        #region Static Fields

        /// <summary>
        /// The logger.
        /// </summary>
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The show.
        /// </summary>
        /// <param name="messageBoxText">
        /// The message box text.
        /// </param>
        /// <returns>
        /// The <see cref="MessageBoxResult"/>.
        /// </returns>
        public static MessageBoxResult Show(string messageBoxText)
        {
            return Show(
                null, 
                messageBoxText, 
                string.Empty, 
                MessageBoxButton.OK, 
                MessageBoxImage.None, 
                MessageBoxResult.None, 
                MessageBoxOptions.None);
        }

        /// <summary>
        /// The show.
        /// </summary>
        /// <param name="messageBoxText">
        /// The message box text.
        /// </param>
        /// <param name="caption">
        /// The caption.
        /// </param>
        /// <returns>
        /// The <see cref="MessageBoxResult"/>.
        /// </returns>
        public static MessageBoxResult Show(string messageBoxText, string caption)
        {
            return Show(
                null, 
                messageBoxText, 
                caption, 
                MessageBoxButton.OK, 
                MessageBoxImage.None, 
                MessageBoxResult.None, 
                MessageBoxOptions.None);
        }

        /// <summary>
        /// The show.
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        /// <param name="messageBoxText">
        /// The message box text.
        /// </param>
        /// <returns>
        /// The <see cref="MessageBoxResult"/>.
        /// </returns>
        public static MessageBoxResult Show(Window owner, string messageBoxText)
        {
            return Show(
                owner, 
                messageBoxText, 
                string.Empty, 
                MessageBoxButton.OK, 
                MessageBoxImage.None, 
                MessageBoxResult.None, 
                MessageBoxOptions.None);
        }

        /// <summary>
        /// The show.
        /// </summary>
        /// <param name="messageBoxText">
        /// The message box text.
        /// </param>
        /// <param name="caption">
        /// The caption.
        /// </param>
        /// <param name="button">
        /// The button.
        /// </param>
        /// <returns>
        /// The <see cref="MessageBoxResult"/>.
        /// </returns>
        public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button)
        {
            return Show(
                null, 
                messageBoxText, 
                caption, 
                button, 
                MessageBoxImage.None, 
                MessageBoxResult.None, 
                MessageBoxOptions.None);
        }

        /// <summary>
        /// The show.
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        /// <param name="messageBoxText">
        /// The message box text.
        /// </param>
        /// <param name="caption">
        /// The caption.
        /// </param>
        /// <returns>
        /// The <see cref="MessageBoxResult"/>.
        /// </returns>
        public static MessageBoxResult Show(Window owner, string messageBoxText, string caption)
        {
            return Show(
                owner, 
                messageBoxText, 
                caption, 
                MessageBoxButton.OK, 
                MessageBoxImage.None, 
                MessageBoxResult.None, 
                MessageBoxOptions.None);
        }

        /// <summary>
        /// The show.
        /// </summary>
        /// <param name="messageBoxText">
        /// The message box text.
        /// </param>
        /// <param name="caption">
        /// The caption.
        /// </param>
        /// <param name="button">
        /// The button.
        /// </param>
        /// <param name="icon">
        /// The icon.
        /// </param>
        /// <returns>
        /// The <see cref="MessageBoxResult"/>.
        /// </returns>
        public static MessageBoxResult Show(
            string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon)
        {
            return Show(null, messageBoxText, caption, button, icon, MessageBoxResult.None, MessageBoxOptions.None);
        }

        /// <summary>
        /// The show.
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        /// <param name="messageBoxText">
        /// The message box text.
        /// </param>
        /// <param name="caption">
        /// The caption.
        /// </param>
        /// <param name="button">
        /// The button.
        /// </param>
        /// <returns>
        /// The <see cref="MessageBoxResult"/>.
        /// </returns>
        public static MessageBoxResult Show(
            Window owner, string messageBoxText, string caption, MessageBoxButton button)
        {
            return Show(
                owner, 
                messageBoxText, 
                caption, 
                button, 
                MessageBoxImage.None, 
                MessageBoxResult.None, 
                MessageBoxOptions.None);
        }

        /// <summary>
        /// The show.
        /// </summary>
        /// <param name="messageBoxText">
        /// The message box text.
        /// </param>
        /// <param name="caption">
        /// The caption.
        /// </param>
        /// <param name="button">
        /// The button.
        /// </param>
        /// <param name="icon">
        /// The icon.
        /// </param>
        /// <param name="defaultResult">
        /// The default result.
        /// </param>
        /// <returns>
        /// The <see cref="MessageBoxResult"/>.
        /// </returns>
        public static MessageBoxResult Show(
            string messageBoxText, 
            string caption, 
            MessageBoxButton button, 
            MessageBoxImage icon, 
            MessageBoxResult defaultResult)
        {
            return Show(null, messageBoxText, caption, button, icon, defaultResult, MessageBoxOptions.None);
        }

        /// <summary>
        /// The show.
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        /// <param name="messageBoxText">
        /// The message box text.
        /// </param>
        /// <param name="caption">
        /// The caption.
        /// </param>
        /// <param name="button">
        /// The button.
        /// </param>
        /// <param name="icon">
        /// The icon.
        /// </param>
        /// <returns>
        /// The <see cref="MessageBoxResult"/>.
        /// </returns>
        public static MessageBoxResult Show(
            Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon)
        {
            return Show(owner, messageBoxText, caption, button, icon, MessageBoxResult.None, MessageBoxOptions.None);
        }

        /// <summary>
        /// The show.
        /// </summary>
        /// <param name="messageBoxText">
        /// The message box text.
        /// </param>
        /// <param name="caption">
        /// The caption.
        /// </param>
        /// <param name="button">
        /// The button.
        /// </param>
        /// <param name="icon">
        /// The icon.
        /// </param>
        /// <param name="defaultResult">
        /// The default result.
        /// </param>
        /// <param name="options">
        /// The options.
        /// </param>
        /// <returns>
        /// The <see cref="MessageBoxResult"/>.
        /// </returns>
        public static MessageBoxResult Show(
            string messageBoxText, 
            string caption, 
            MessageBoxButton button, 
            MessageBoxImage icon, 
            MessageBoxResult defaultResult, 
            MessageBoxOptions options)
        {
            return Show(null, messageBoxText, caption, button, icon, defaultResult, options);
        }

        /// <summary>
        /// The show.
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        /// <param name="messageBoxText">
        /// The message box text.
        /// </param>
        /// <param name="caption">
        /// The caption.
        /// </param>
        /// <param name="button">
        /// The button.
        /// </param>
        /// <param name="icon">
        /// The icon.
        /// </param>
        /// <param name="defaultResult">
        /// The default result.
        /// </param>
        /// <returns>
        /// The <see cref="MessageBoxResult"/>.
        /// </returns>
        public static MessageBoxResult Show(
            Window owner, 
            string messageBoxText, 
            string caption, 
            MessageBoxButton button, 
            MessageBoxImage icon, 
            MessageBoxResult defaultResult)
        {
            return Show(owner, messageBoxText, caption, button, icon, defaultResult, MessageBoxOptions.None);
        }

        /// <summary>
        /// The show.
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        /// <param name="messageBoxText">
        /// The message box text.
        /// </param>
        /// <param name="caption">
        /// The caption.
        /// </param>
        /// <param name="button">
        /// The button.
        /// </param>
        /// <param name="icon">
        /// The icon.
        /// </param>
        /// <param name="defaultResult">
        /// The default result.
        /// </param>
        /// <param name="options">
        /// The options.
        /// </param>
        /// <returns>
        /// The <see cref="MessageBoxResult"/>.
        /// </returns>
        public static MessageBoxResult Show(
            Window owner, 
            string messageBoxText, 
            string caption, 
            MessageBoxButton button, 
            MessageBoxImage icon, 
            MessageBoxResult defaultResult, 
            MessageBoxOptions options)
        {
            Logger.Debug(
                "Method: Show - Owner: {0}, MessageBoxText: {1}, Caption: {2}, Button: {3}, Image: {4}, DefaultResult: {5}, Options: {6}", 
                owner, 
                messageBoxText, 
                caption, 
                button.ToString(), 
                icon.ToString(), 
                defaultResult.ToString(), 
                options.ToString());

            Message messageBox = new Message();

            // Handle owner
            if (owner != null)
            {
                messageBox.Owner = owner;
            }
            else
            {
                messageBox.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            }

            // Handle caption
            messageBox.MessageBoxCaption.Text = !string.IsNullOrEmpty(caption) ? caption : string.Empty;

            // Handle buttons
            messageBox.MessageBoxText.Text = messageBoxText;

            switch (button)
            {
                case MessageBoxButton.OK:
                    messageBox.Button1.Content = "OK";

                    messageBox.Button2.Visibility = Visibility.Collapsed;
                    messageBox.Button3.Visibility = Visibility.Collapsed;
                    break;
                case MessageBoxButton.OKCancel:
                    messageBox.Button1.Content = "OK";

                    messageBox.Button2.Content = "Cancel";
                    messageBox.Button3.Visibility = Visibility.Collapsed;
                    break;
                case MessageBoxButton.YesNo:
                    messageBox.Button3.Visibility = Visibility.Collapsed;
                    break;
            }

            // Handle icon
            switch (icon)
            {
                case MessageBoxImage.None:
                    messageBox.SetIcon(null);
                    break;
                case MessageBoxImage.Hand:
                    messageBox.SetIcon("error");
                    break;
                case MessageBoxImage.Question:
                    messageBox.SetIcon("question");
                    break;
                case MessageBoxImage.Exclamation:
                    messageBox.SetIcon("warning2");
                    break;
                case MessageBoxImage.Asterisk:
                    messageBox.SetIcon("info");
                    break;
            }

            // Handling options
            switch (options)
            {
                case MessageBoxOptions.ServiceNotification:
                case MessageBoxOptions.DefaultDesktopOnly:
                    if (owner != null)
                    {
                        System.Windows.MessageBox.Show(
                            owner, messageBoxText, caption, button, icon, defaultResult, options);
                    }

                    break;
                case MessageBoxOptions.RightAlign:
                    messageBox.MessageBoxCaption.HorizontalAlignment = HorizontalAlignment.Right;
                    messageBox.MessageBoxText.HorizontalAlignment = HorizontalAlignment.Right;

                    break;
                case MessageBoxOptions.RtlReading:
                    messageBox.MessageBoxCaption.FlowDirection = FlowDirection.RightToLeft;
                    messageBox.MessageBoxText.FlowDirection = FlowDirection.RightToLeft;
                    messageBox.MessageBoxIcon.FlowDirection = FlowDirection.RightToLeft;
                    messageBox.FlowDirection = FlowDirection.RightToLeft;

                    break;
            }

            // Display MessageBox
            messageBox.ShowDialog();

            // Handling default result
            MessageBoxResult result = messageBox.DialogResult;

            if ((defaultResult != MessageBoxResult.None) && (result == MessageBoxResult.None))
            {
                return defaultResult;
            }

            // Handling other results
            switch (button)
            {
                case MessageBoxButton.OK:
                    return MessageBoxResult.OK;
                case MessageBoxButton.OKCancel:
                    return (result == MessageBoxResult.Yes) ? MessageBoxResult.OK : MessageBoxResult.Cancel;
                default:
                    return result;
            }
        }

        #endregion
    }
}